﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Project;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition;

namespace PSLib.WOWAddon.Base
{
    /// <summary>
	/// Entry point for WOW Addon package
	/// </summary>
	//Registers for project factory
    [ProvideProjectFactory(typeof(WOWAddonProjectFactory), null, "WOW Addon Project Files (*.wowaddonproj);*.wowaddonproj", "wowaddonproj", "wowaddonproj", @".\\NullPath", LanguageVsTemplate = "WOWAddonProject")]
	//Registers the property page class GeneralPropertyPage with COM.
	[ProvideObject(typeof(GeneralPropertyPage))]

	[Guid(GuidList.guidProjectPkgString)]
	public class WOWAddonProjectPackage : ProjectPackage, IVsInstalledProduct, IOleComponent
	{
		#region Constructor

		public WOWAddonProjectPackage()
		{
			//AggregateCatalog catalog = new AggregateCatalog();
			//catalog.Catalogs.Add(new AssemblyCatalog(typeof(Definitions.Test).Assembly));
			//CompositionContainer container = new CompositionContainer(catalog);
            
			//CompositionBatch batch = new CompositionBatch();
			//batch.AddPart(container);
			//container.Compose(batch);

			//MyClass1 myProgram = new MyClass1();
			//container.SatisfyImportsOnce(myProgram);
			//myProgram.MyClass.TestProp = "Here in project";

			//MyClass1 myProgram2 = new MyClass1();
			//container.SatisfyImportsOnce(myProgram2);
		}

		#endregion

		#region IVsInstalledProduct Members

		public int IdBmpSplash(out uint pIdBmp)
		{
			pIdBmp = 0;

			return VSConstants.S_OK;
		}

		public int IdIcoLogoForAboutbox(out uint pIdIco)
		{
			pIdIco = 0;
			return VSConstants.S_OK;
		}

		public int OfficialName(out string pbstrName)
		{
			pbstrName = GetResourceString("@ProductName");
			return VSConstants.S_OK;
		}

		public int ProductDetails(out string pbstrProductDetails)
		{
			pbstrProductDetails = GetResourceString("@ProductDetails");
			return VSConstants.S_OK;
		}

		public int ProductID(out string pbstrPID)
		{
			pbstrPID = GetResourceString("@ProductID");
			return VSConstants.S_OK;
		}

		#endregion

		#region IOleComponent Members

		public int FContinueMessageLoop(uint uReason, IntPtr pvLoopData, MSG[] pMsgPeeked)
		{
			return 1;
		}

		public int FDoIdle(uint grfidlef)
		{
			//if (null != libraryManager)
			//{
			//    libraryManager.OnIdle();
			//}
            return 0;
		}

		public int FPreTranslateMessage(MSG[] pMsg)
		{
			return 0;
		}

		public int FQueryTerminate(int fPromptUser)
		{
			return 1;
		}

		public int FReserved1(uint dwReserved, uint message, IntPtr wParam, IntPtr lParam)
		{
			return 1;
		}

		public IntPtr HwndGetWindow(uint dwWhich, uint dwReserved)
		{
			return IntPtr.Zero;
		}

		public void OnActivationChange(IOleComponent pic, int fSameComponent, OLECRINFO[] pcrinfo, int fHostIsActivating, OLECHOSTINFO[] pchostinfo, uint dwReserved)
		{
		}

		public void OnAppActivate(int fActive, uint dwOtherThreadID)
		{
		}

		public void OnEnterState(uint uStateID, int fEnter)
		{
		}

		public void OnLoseActivation()
		{
		}

		public void Terminate()
		{
		}

		#endregion

		#region ProjectPackage overrides

		public override string ProductUserContext
		{
			get { throw new NotImplementedException(); }
		}

		protected override void Initialize()
		{
			base.Initialize();
			this.RegisterProjectFactory(new WOWAddonProjectFactory(this));
		}

		#endregion

		#region Private methods
		/// <summary>
		/// This method loads a localized string based on the specified resource.
		/// </summary>
		/// <param name="resourceName">Resource to load</param>
		/// <returns>String loaded for the specified resource</returns>
		public string GetResourceString(string resourceName)
		{
			string resourceValue;
			IVsResourceManager resourceManager = (IVsResourceManager)GetService(typeof(SVsResourceManager));
			if (resourceManager == null)
			{
				throw new InvalidOperationException("Could not get SVsResourceManager service. Make sure the package is Sited before calling this method");
			}
			Guid packageGuid = this.GetType().GUID;
			int hr = resourceManager.LoadResourceString(ref packageGuid, -1, resourceName, out resourceValue);
			Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(hr);
			return resourceValue;
		}

		#endregion
	}
}
